package com.entrogy.service.impl;

import com.entrogy.bean.*;
import com.entrogy.dao.CourseSystemMapper;
import com.entrogy.dao.UserMapper;
import com.entrogy.service.CourseSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CourseSystemServiceImpl
 * @Description
 * @Author luoxingjiang
 * @Date 2019-11-19 下午 6:29
 * @Version 1.0
 **/
@Service
@Transactional
public class CourseSystemServiceImpl implements CourseSystemService {

    @Autowired
    private CourseSystemMapper courseSystemMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insertCourse(CourseSystemCourseEntity course) {
        // 比较安排的课时和上课时间课时是否一致
        boolean checkResult = checkCourseEquals(course);
        if (!checkResult){
            return 1;
        }
        if (course.getClassLimitCount() != null && course.getCourseLimitCount() != null && course.getClassLimitCount() > course.getCourseLimitCount()){
            return 2;
        }
        if (course.getClassLimitCount() != null && course.getCourseLimitList().size() == 0){
            return 3;
        }
        // 首先添加课程
        int insertCourseRow = courseSystemMapper.insertCourse(course);
        Integer courseId = course.getId();
        String createUserId = course.getCreateUserId();
        // 添加课程时间安排信息
        List<CourseSystemCourseTimeEntity> courseTimeList = course.getCourseTimeList();
        for (CourseSystemCourseTimeEntity courseTime : courseTimeList) {
            // 首先插入时间安排表
            courseTime.setCourseId(courseId).setCreateUserId(createUserId);
            int insertCourseTimeRow = courseSystemMapper.insertCourseTime(courseTime);
            // 然后插入上课顺序表
            Integer courseTimeId = courseTime.getId();
            List<Integer> courseOrderList = courseTime.getCourseOrderList();
            for (Integer courseOrder : courseOrderList) {
                int insertCourseOrderListRow = courseSystemMapper.insertCourseOrder(courseTimeId, courseOrder, createUserId);
            }
        }
        // 添加教师课程安排
        List<CourseSystemCourseArrangeEntity> courseArrangeList = course.getCourseArrangeList();
        for (CourseSystemCourseArrangeEntity arrange : courseArrangeList) {
            arrange.setCreateUserId(createUserId).setCourseId(courseId);
            int insertTeacherCourse = courseSystemMapper.insertCourseArrange(arrange);
        }
        // 添加课程限制
        List<CourseSystemCourseLimitEntity> courseLimitList = course.getCourseLimitList();
        if (courseLimitList.size() > 0){
            for (CourseSystemCourseLimitEntity courseLimit : courseLimitList) {
                courseLimit.setCourseId(courseId).setCreateUserId(createUserId);
            }
            int insertCourseLimitRow = courseSystemMapper.insertCourseLimit(courseLimitList);
        }
        return 0;
    }

    @Override
    public boolean deleteCourse(Integer courseId) {
        // 首先删除课程
        int deleteCourseRow = courseSystemMapper.deleteCourse(courseId);
        // 然后删除课程周期安排信息
        int deleteCourseTimeRow = courseSystemMapper.deleteCourseTime(courseId);
        // 删除课程顺序信息
        int deleteCourseOrderRow = courseSystemMapper.deleteCourseOrder(courseId);
        // 最后删除课程教师安排信息
        int deleteCourseTeacher = courseSystemMapper.deleteCourseTeacher(courseId);
        // 删除课程限制
        int deleteCourseLimit = courseSystemMapper.deleteCourseLimit(courseId);
        return true;
    }

    @Override
    public List<CourseSystemCourseEntity> listCourse(Integer courseId, String courseName, Integer schoolCode, Boolean isFinish, String teacherName, Integer enableStatus) {
        return courseSystemMapper.listCourse(courseId, courseName, schoolCode, isFinish, teacherName, enableStatus);
    }

    @Override
    public List<CourseSystemCourseTimeEntity> listCourseTime(Integer courseId) {
        List<CourseSystemCourseTimeEntity> courseTimeList = courseSystemMapper.listCourseTime(courseId);
        for (CourseSystemCourseTimeEntity courseTime : courseTimeList) {
            courseTime.setCourseOrderList(courseSystemMapper.listCourseOrder(courseTime.getId()));
        }
        return courseTimeList;
    }


    @Override
    public Integer updateCourse(CourseSystemCourseEntity course) {
        // 首先判断这些数据能不能修改
        boolean checkResult = checkCourseEquals(course);
        if (!checkResult){
            return 1;
        }

        // 查询出该课程的信息
        Integer courseId = course.getId();
        List<CourseSystemCourseEntity> courseDbList = courseSystemMapper.listCourse(courseId, null, null, null, null, null);
        if(courseDbList.size() > 1){
            return 2;
        }
        CourseSystemCourseEntity courseDb = courseDbList.get(0);
        int courseLimitCount = courseDb.getCourseLimitCount() == null ? Integer.MAX_VALUE : courseDb.getCourseLimitCount();
        // 课程限制人数减少后需要判断当前是否报名数已经大于课程限制人数
        if (course.getCourseLimitCount() != null && course.getCourseLimitCount() < courseLimitCount){
            int hasChooseCourseStudentCount = courseSystemMapper.countChooseStudent(courseId, null);
            if (hasChooseCourseStudentCount > course.getCourseLimitCount()){
                return 3;
            }
        }
        if (course.getClassLimitCount() != null && course.getCourseLimitCount() != null && course.getClassLimitCount() > course.getCourseLimitCount()){
            return 4;
        }
        // 设置了班级限报的话也必须设置授课对象
        if (course.getClassLimitCount() != null && course.getClassLimitCount() != 0 && course.getCourseLimitList().size() == 0){
            return 5;
        }
        List<CourseSystemCourseLimitEntity> courseLimitList = course.getCourseLimitList();
        int updateCourseRow = courseSystemMapper.updateCourse(course);
        List<CourseSystemCourseTimeEntity> courseTimeList = course.getCourseTimeList();

        if (courseTimeList.size() > 0){
            // 更改了课程时间安排信息需要先删除在插入
            int deleteCourseTimeRow = courseSystemMapper.deleteCourseTime(courseId);
            for (CourseSystemCourseTimeEntity courseTime : courseTimeList) {
                // 首先插入时间安排表
                courseTime.setCourseId(courseId).setCreateUserId(course.getOperateUserId());
                int insertCourseTimeRow = courseSystemMapper.insertCourseTime(courseTime);
                // 然后插入上课顺序表
                Integer courseTimeId = courseTime.getId();
                List<Integer> courseOrderList = courseTime.getCourseOrderList();
                for (Integer courseOrder : courseOrderList) {
                    int insertCourseOrderListRow = courseSystemMapper.insertCourseOrder(courseTimeId, courseOrder, course.getOperateUserId());
                }
            }
        }
        List<CourseSystemCourseArrangeEntity> courseArrangeList = course.getCourseArrangeList();
        if (courseArrangeList.size() > 0){
            // 更改了课程教师安排需要先删除后插入
            int deleteCourseTeacher = courseSystemMapper.deleteCourseTeacher(courseId);
            for (CourseSystemCourseArrangeEntity arrange : courseArrangeList) {
                arrange.setCourseId(courseId).setCreateUserId(course.getOperateUserId());
                int insertTeacherCourse = courseSystemMapper.insertCourseArrange(arrange);
            }
        }
        int deleteCourseLimit = courseSystemMapper.deleteCourseLimit(courseId);
        if (courseLimitList.size() > 0){
            for (CourseSystemCourseLimitEntity courseLimit : courseLimitList) {
                courseLimit.setCourseId(courseId).setCreateUserId(course.getOperateUserId());
            }
            int insertCourseLimit = courseSystemMapper.insertCourseLimit(courseLimitList);
        }
        return 0;
    }

    @Override
    public List<CourseSystemCourseArrangeEntity> listCourseTeacherArrange(Integer courseId) {
        return courseSystemMapper.listCourseTeacherArrange(courseId);
    }

    @Override
    public List<CourseSystemCourseLimitEntity> listCourseLimit(Integer courseId, Integer schoolCode) {
        return courseSystemMapper.listCourseLimit(courseId, schoolCode);
    }

    @Override
    public List<CourseSystemCourseEntity> listTeacherCourse(String teacherId, String courseName, Boolean isFinish) {
        return courseSystemMapper.listTeacherCourse(teacherId, courseName, isFinish);
    }

    @Override
    public List<ClassStudentEntity> listCourseStudent(Integer courseId, String studentName) {
        return courseSystemMapper.listCourseStudent(courseId, studentName, null);
    }

    @Override
    public boolean chooseCourse(String uid, Integer courseId, Integer studentId) {
        int insertCourseChooseRow = courseSystemMapper.insertCourseChoose(uid, courseId, studentId);
        return insertCourseChooseRow > 1;
    }

    @Override
    public List<CourseSystemCourseEntity> listStudentCourse(Integer studentId) {
        return courseSystemMapper.listStudentCourse(studentId);
    }

    @Override
    public List<CourseSystemCourseEntity> listTeacherCreateCourse(String uid, String courseName, Boolean isFinish, Integer enableStatus) {
        return courseSystemMapper.listTeacherCreateCourse(uid, courseName, isFinish, enableStatus);
    }

    @Override
    public boolean enableCourse(Integer courseId, Integer enableStatus) {
        CourseSystemCourseEntity course = new CourseSystemCourseEntity();
        course.setId(courseId).setEnableStatus(enableStatus);
        return courseSystemMapper.updateCourse(course) > 0;
    }

    @Override
    public boolean teacherGiveScore(List<CourseSystemStudentScoreEntity> studentScoreList) {
        int updateRow = 0;
        for (CourseSystemStudentScoreEntity studentScore : studentScoreList) {
            CourseSystemChooseCourseEntity chooseCourse = new CourseSystemChooseCourseEntity();
            chooseCourse.setId(studentScore.getCourseChooseId())
                    .setCourseId(studentScore.getCourseId())
                    .setScore(studentScore.getScore())
                    .setTeacherEvaluate(studentScore.getTeacherEvaluate());
            int row = courseSystemMapper.updateChooseCourse(chooseCourse);
            if (row > 0){
                updateRow ++;
            }
        }
        return updateRow == studentScoreList.size();
    }

    @Override
    public boolean deleteStudent(Integer courseChooseId) {
        CourseSystemChooseCourseEntity chooseCourse = new CourseSystemChooseCourseEntity();
        chooseCourse.setId(courseChooseId).setDeleteStatus(1);
        return courseSystemMapper.updateChooseCourse(chooseCourse) > 0;
    }

    @Override
    public List<StudentBasicInfoEntity> listStudentCheckRecord(Integer courseId, Integer weekOrder, Integer dayOfWeek, Integer courseOrder, String studentName, Integer studentId) {
        List<ClassStudentEntity> classList = courseSystemMapper.listCourseStudent(courseId, studentName, studentId);
        List<StudentBasicInfoEntity> studentList = new ArrayList<>();
        for (ClassStudentEntity classStudentEntity : classList) {
            List<StudentBasicInfoEntity> tempStudentList = classStudentEntity.getStudentList();
            for (StudentBasicInfoEntity student : tempStudentList) {
                List<CourseSystemAttendanceEntity> courseAttendanceList = courseSystemMapper.listStudentAttendance(student.getId(), courseId, weekOrder, dayOfWeek, courseOrder);
                student.setCourseSystemAttendanceList(courseAttendanceList);
                studentList.add(student);
            }
        }
        return studentList;
    }

    @Override
    public boolean checkStudent(List<CourseSystemAttendanceEntity> courseAttendanceList) {
        List<CourseSystemAttendanceEntity> insertCourseAttendanceList = new ArrayList<>();
        int updateCount = 0;
        for (CourseSystemAttendanceEntity courseAttendance : courseAttendanceList) {
            Integer courseAttendanceId = courseAttendance.getId();
            if (courseAttendanceId == null || courseAttendanceId == 0){
                insertCourseAttendanceList.add(courseAttendance);
            }else {
                int updateRow = courseSystemMapper.updateCheckStudentStatus(courseAttendance);
                if (updateRow > 0){
                    updateCount ++;
                }
            }
        }
        int row = 0;
        if (insertCourseAttendanceList.size() > 0){
            row = courseSystemMapper.insertStudentAttendanceRecord(insertCourseAttendanceList);
        }
        return row == insertCourseAttendanceList.size() && updateCount == courseAttendanceList.size() - insertCourseAttendanceList.size();
    }

    @Override
    public boolean updateCheckStudentStatus(CourseSystemAttendanceEntity courseAttendance) {
        return courseSystemMapper.updateCheckStudentStatus(courseAttendance) > 0;
    }

    @Override
    public int countChooseStudent(Integer courseId, Integer classId) {
        return courseSystemMapper.countChooseStudent(courseId, classId);
    }

    @Override
    public List<CourseSystemParentCourseEntity> listParentOrStudentCourse(String uid, Integer studentId, String courseName, String teacherName, Boolean isFinish) {
        return courseSystemMapper.listParentOrStudentCourse(uid, studentId, courseName, teacherName, isFinish);
    }

    @Override
    public boolean dropCourse(Integer courseChooseId) {
        CourseSystemChooseCourseEntity courseChoose = new CourseSystemChooseCourseEntity();
        courseChoose.setDropCourse(1).setId(courseChooseId);
        return courseSystemMapper.updateChooseCourse(courseChoose) > 0;
    }

    @Override
    public boolean evaluateTeacher(Integer courseChooseId, String evaluateTeacher, Integer parentScore) {
        CourseSystemChooseCourseEntity courseChoose = new CourseSystemChooseCourseEntity();
        courseChoose.setEvaluateTeacher(evaluateTeacher).setId(courseChooseId).setParentScore(parentScore);
        return courseSystemMapper.updateChooseCourse(courseChoose) > 0;
    }

    @Override
    public List<StudentBasicInfoEntity> listCourseStudentNotGroup(Integer courseId) {
        return courseSystemMapper.listCourseStudentNotGroup(courseId);
    }

    @Override
    public CourseSystemStudentScoreEntity getStudentChooseInfo(Integer studentId, Integer courseId) {
        return courseSystemMapper.getStudentChooseInfo(studentId, courseId);
    }

    @Override
    public CourseSystemChooseEvaluateEntity getCourseChooseInfo(Integer courseChooseId) {
        return courseSystemMapper.getCourseChooseInfo(courseChooseId);
    }

    /**
     * 比较安排的课时和上课时间课时是否一致
     * @param course
     * @return
     */
    private boolean checkCourseEquals(CourseSystemCourseEntity course){
        // 安排的课时总数=每周安排的课时数X周数
        int arrangeCourseCount = 0;
        List<CourseSystemCourseTimeEntity> courseTimeList = course.getCourseTimeList();
        for (CourseSystemCourseTimeEntity courseTime : courseTimeList) {
            List<Integer> courseOrderList = courseTime.getCourseOrderList();
            arrangeCourseCount += courseOrderList.size();
        }
        if (arrangeCourseCount * course.getWeekNumber() != course.getCourseCount()){
            return false;
        }else {
            return true;
        }
    }

}
